package mortal.game.control
{
    import Message.Db.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.serialization.json.*;
    import flash.events.*;
    import flash.utils.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.view.equipment.*;
    import mortal.game.view.equipment.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class EquipmentController extends Controller
    {
        private var _equipmentModule:EquipmentModule;
        private var _opertionType:int = 0;
        private var _equipmentType:int = 0;
        private var _materialType:int = 0;
        private var _cost:int = 0;
        private var _successRate:int = 0;
        private var _choosedEquipmentData:Array;
        private var _choosedMaterial:Array;
        private var _useBind:Boolean = true;
        private var _useLuckTuneNum:int = 0;
        private var _currentUpdateType:int = 0;

        public function EquipmentController()
        {
            this._choosedEquipmentData = [];
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateCost);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            Dispatcher.addEventListener(EventName.Equipment_UpdateResult, this.onUpdateResult);
            Dispatcher.addEventListener(EventName.Equipment_UpdateError, this.onUpdateError);
            Dispatcher.addEventListener(EventName.Equipment_DoCast_Strengthen, this.doCastHandler);
            Dispatcher.addEventListener(EventName.Equipment_DoCast_Refine, this.doCastHandler);
            Dispatcher.addEventListener(EventName.Equipment_DoCast_Recast, this.doCastHandler);
            Dispatcher.addEventListener(EventName.Equipment_DoCast_MakeHole, this.doCastHandler);
            Dispatcher.addEventListener(EventName.Equipment_DoCast_EmbedJewel, this.doCastHandler);
            Dispatcher.addEventListener(EventName.Equipment_DoCast_RemoveJewel, this.doCastHandler);
            Dispatcher.addEventListener(EventName.Equipment_DoCast_ComposeJewel, this.doCastHandler);
            Dispatcher.addEventListener(EventName.Guide_EquipBuild, this.onGuideEquipBuildHandler);
            
        }

        public function set currentUpdateType(param1:int) : void
        {
            this._currentUpdateType = param1;
            
        }

        public function get choosedEquipmentData() : Array
        {
            return this._choosedEquipmentData;
        }

        public function get equipmentModule() : EquipmentModule
        {
            return this._equipmentModule;
        }

        override protected function initView() : IView
        {
            if (!this._equipmentModule)
            {
                this._equipmentModule = new EquipmentModule();
                this._equipmentModule.addEventListener(WindowEvent.SHOW, this.onEquimentShowHandler);
                this._equipmentModule.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._equipmentModule;
        }

        private function addListeners() : void
        {
            view.addEventListener(EquipmentModule.Event_OpertionTypeChange, this.opertionTypeChangeHandler);
            view.addEventListener(EquipmentModule.Event_EquipmentTypeChange, this.equipmentTypeChangeHandler);
            view.addEventListener(EquipmentModule.Event_MaterialTypeChange, this.materialTypeChangeHandler);
            view.addEventListener(EquipmentModule.Event_ChoosedEquimentChange, this.choosedEquimentChangeHandler);
            view.addEventListener(EquipmentModule.Event_ChoosedMaterialChange, this.choosedMaterialChangeHandler);
            view.addEventListener(EquipmentModule.Event_DoBuyEvent, this.buyMaterialHandler);
            view.addEventListener(EquipmentModule.Event_UseBindChange, this.useBindChangeHandler);
            view.addEventListener(EquipmentModule.Event_CostChange, this.costChangeHandler);
            view.addEventListener(EquipmentModule.Event_DoUpdateEvent, this.doUpdate);
            view.addEventListener(EquipmentModule.Event_LuckTuneNumChange, this.LuckTuneNumChangeHandler);
            view.addEventListener(EquipmentModule.Event_ReChooseJewels, this.ReChooseJewels);
            view.addEventListener(EquipmentModule.Event_AddHintLabel, this.doAddHintLabel);
            
        }

        private function removeListeners() : void
        {
            view.removeEventListener(EquipmentModule.Event_OpertionTypeChange, this.opertionTypeChangeHandler);
            view.removeEventListener(EquipmentModule.Event_EquipmentTypeChange, this.equipmentTypeChangeHandler);
            view.removeEventListener(EquipmentModule.Event_MaterialTypeChange, this.materialTypeChangeHandler);
            view.removeEventListener(EquipmentModule.Event_ChoosedEquimentChange, this.choosedEquimentChangeHandler);
            view.removeEventListener(EquipmentModule.Event_ChoosedMaterialChange, this.choosedMaterialChangeHandler);
            view.removeEventListener(EquipmentModule.Event_DoBuyEvent, this.buyMaterialHandler);
            view.removeEventListener(EquipmentModule.Event_UseBindChange, this.useBindChangeHandler);
            view.removeEventListener(EquipmentModule.Event_CostChange, this.costChangeHandler);
            view.removeEventListener(EquipmentModule.Event_DoUpdateEvent, this.doUpdate);
            view.removeEventListener(EquipmentModule.Event_LuckTuneNumChange, this.LuckTuneNumChangeHandler);
            view.removeEventListener(EquipmentModule.Event_ReChooseJewels, this.ReChooseJewels);
            view.removeEventListener(EquipmentModule.Event_AddHintLabel, this.doAddHintLabel);
            
        }

        private function serverBagDataChangehandler(param1:Object = null) : void
        {
            if (_view)
            {
                this.updateEquipment();
                if (this._materialType == EquipmentModule.MaterialType_Use)
                {
                    this.updateMaterial();
                }
                else if (this._materialType == EquipmentModule.MaterialType_Jewel)
                {
                    this.updateMaterialJewel();
                }
                this.autoSelectedMaterial();
                this.checkEquipLost();
            }
            
        }

        private function opertionTypeChangeHandler(event:DataEvent) : void
        {
            this._opertionType = Number(event.data);
            this._choosedEquipmentData = [];
            this._cost = 0;
            this.updateEquipment();
            this.updateCost();
            if (this._materialType == EquipmentModule.MaterialType_Use)
            {
                this.updateMaterial();
            }
            else if (this._materialType == EquipmentModule.MaterialType_Jewel)
            {
                this.updateMaterialJewel();
            }
            
        }

        private function buyMaterialHandler(event:DataEvent) : void
        {
            var info:ShopItemInfo;
            var amount:int;
            var cost:int;
            var color:ColorInfo;
            var htmlText:String;
            var e:* = event;
            if (cache.equipment.isBuyDirectly)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, e.data));
            }
            else
            {
                info = e.data.shopItemInfo as ShopItemInfo;
                amount = e.data.amount as int;
                cost = info.tshopSell.price * amount;
                color = ColorConfig.instance.getItemColor(info.itemInfo.item.color);
                htmlText = "        您将购买" + HTMLUtil.addColor(amount + "", "#FF0000") + "个" + HTMLUtil.addColor(info.itemInfo.item.name, color.color) + "，\n共消耗" + HTMLUtil.addColor(cost + GameDefConfig.instance.getEPrictUnitName(info.unit), "#FFEA00") + "。确定购买吗？";
                Alert.show(htmlText, "提示", Alert.OK | Alert.CANCEL, null, function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, e.data));
                }
                
            }
            );
            }
            
        }

        private function costChangeHandler(event:DataEvent) : void
        {
            this._cost = Number(event.data);
            this.updateCost();
            
        }

        private function useBindChangeHandler(event:DataEvent) : void
        {
            this._useBind = Boolean(event.data);
            this.autoSelectedMaterial();
            
        }

        private function equipmentTypeChangeHandler(event:DataEvent) : void
        {
            this._equipmentType = Number(event.data);
            this.updateEquipment();
            
        }

        private function materialTypeChangeHandler(event:DataEvent) : void
        {
            this._materialType = Number(event.data);
            if (this._materialType == EquipmentModule.MaterialType_Use)
            {
                this.updateMaterial();
            }
            else if (this._materialType == EquipmentModule.MaterialType_Jewel)
            {
                this.updateMaterialJewel();
            }
            
        }

        private function choosedMaterialChangeHandler(event:DataEvent) : void
        {
            this._choosedMaterial = event.data as Array;
            if (this._opertionType == EquipmentModule.OperationType_EmbedJewel || this._opertionType == EquipmentModule.OperationType_RemoveJewel)
            {
                this.autoSelectedMaterial();
            }
            else if (this._opertionType == EquipmentModule.OperationType_ComposeJewel)
            {
            }
            
        }

        private function choosedEquimentChangeHandler(event:DataEvent) : void
        {
            this._choosedEquipmentData = event.data as Array;
            if (this._choosedEquipmentData.length > 0)
            {
                this.updateEquipment();
                this.autoSelectedMaterial();
            }
            else
            {
                this.updateEquipment();
            }
            
        }

        private function LuckTuneNumChangeHandler(event:DataEvent) : void
        {
            this._useLuckTuneNum = Number(event.data);
            this.autoSelectedMaterial();
            
        }

        private function onEquimentShowHandler(event:Event) : void
        {
            this.addListeners();
            this.showToPanel(EquipmentModule.OperationType_Strenthen);
            this.updateCost();
            this.updateEquipment();
            this.updateMaterial();
            this._equipmentModule.checkGuide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_WindowOpen));
            
        }

        private function onClose(event:Event) : void
        {
            this.removeListeners();
            this._choosedEquipmentData = [];
            this._choosedMaterial = [];
            
        }

        private function updateEquipment() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Array = null;
            var _loc_3:ItemData = null;
            if (this._opertionType == EquipmentModule.OperationType_ComposeJewel)
            {
                _loc_2 = Cache.instance.equipment.getJewelData(this._opertionType, this._equipmentType);
            }
            else if (this._equipmentType == EquipmentModule.EquipmentType_RefreshProp)
            {
                _loc_2 = Cache.instance.equipment.getMaterialData(this._opertionType);
            }
            else
            {
                _loc_2 = Cache.instance.equipment.getEquipmentData(this._opertionType, this._equipmentType);
            }
            if (this._opertionType == EquipmentModule.OperationType_ComposeItems)
            {
                _loc_1 = _loc_2.length - 1;
                while (_loc_1 >= 0)
                {
                    
                    _loc_3 = _loc_2[_loc_1]["data"] as ItemData;
                    if (_loc_3.itemInfo.item.color == 1 || _loc_3.itemInfo.item.color == 4)
                    {
                        _loc_2.splice(_loc_1, 1);
                    }
                    _loc_1 = _loc_1 - 1;
                }
            }
            if (this._choosedEquipmentData.length > 0)
            {
                _loc_1 = 0;
                while (_loc_1 < _loc_2.length)
                {
                    
                    if (this.checkIsChoosed(_loc_2[_loc_1].data))
                    {
                        _loc_2[_loc_1].used = true;
                    }
                    _loc_1++;
                }
            }
            _view.update({type:"updateEquipment", data:_loc_2});
            
        }

        private function checkEquipLost() : void
        {
            var _loc_1:Boolean = false;
            var _loc_2:Array = null;
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:Array = null;
            var _loc_6:ItemData = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            if (this._choosedEquipmentData.length > 0)
            {
                _loc_2 = cache.pack.rolePackCache.getEquipItems();
                _loc_3 = cache.pack.backPackCache.getEquipItems();
                if (this._opertionType == EquipmentModule.OperationType_EquipDecompose)
                {
                    _loc_2 = [];
                }
                _loc_4 = _loc_3.concat(_loc_2);
                _loc_5 = [];
                _loc_7 = 0;
                while (_loc_7 < this._choosedEquipmentData.length)
                {
                    
                    _loc_1 = false;
                    _loc_6 = this._choosedEquipmentData[_loc_7] as ItemData;
                    _loc_8 = 0;
                    while (_loc_8 < _loc_4.length)
                    {
                        
                        if (_loc_6.uid == (_loc_4[_loc_8] as ItemData).uid)
                        {
                            _loc_1 = true;
                            break;
                        }
                        _loc_8++;
                    }
                    if (!_loc_1)
                    {
                        _loc_5.push(_loc_6);
                    }
                    _loc_7++;
                }
                if (_loc_5.length > 0)
                {
                    _view.update({type:"equipmentLost", data:_loc_5});
                }
            }
            
        }

        private function updateMaterial() : void
        {
            var _loc_3:Array = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:MaterialItemData = null;
            var _loc_8:ItemData = null;
            var _loc_9:int = 0;
            var _loc_10:SPlayerItem = null;
            var _loc_11:ShopItemInfo = null;
            var _loc_1:Array = [];
            var _loc_2:* = new Dictionary();
            _loc_3 = Cache.instance.equipment.getMaterialData(this._opertionType);
            _loc_4 = Cache.instance.equipment.getMaterialBuyData(this._opertionType);
            _loc_5 = 0;
            while (_loc_5 < _loc_3.length)
            {
                
                _loc_8 = _loc_3[_loc_5]["data"] as ItemData;
                if (_loc_8.itemInfo.item.bind == EBind._EBindYes)
                {
                    _loc_9 = _loc_8.itemInfo.item.codeUnbind;
                }
                else
                {
                    _loc_9 = _loc_8.itemInfo.item.code;
                }
                if (_loc_9 == 0)
                {
                }
                else if (_loc_2[_loc_9])
                {
                    (_loc_2[_loc_9] as MaterialItemData).itemData.itemAmount = (_loc_2[_loc_9] as MaterialItemData).itemData.itemAmount + _loc_8.itemAmount;
                }
                else
                {
                    _loc_7 = new MaterialItemData();
                    _loc_10 = new SPlayerItem();
                    _loc_10.itemCode = _loc_9;
                    _loc_7.itemData = new ItemData(_loc_10);
                    _loc_7.itemData.itemAmount = _loc_8.itemAmount;
                    _loc_7.itemData.uid = _loc_8.uid;
                    _loc_2[_loc_9] = _loc_7;
                }
                _loc_5++;
            }
            _loc_5 = 0;
            while (_loc_5 < _loc_4.length)
            {
                
                _loc_11 = _loc_4[_loc_5]["data"];
                if (_loc_2[_loc_11.itemInfo.item.code])
                {
                    _loc_7 = _loc_2[_loc_11.itemInfo.item.code] as MaterialItemData;
                    _loc_7.isInShop = true;
                    _loc_7.shopItem = _loc_11;
                }
                else
                {
                    _loc_7 = new MaterialItemData();
                    _loc_7.isInShop = true;
                    _loc_7.shopItem = _loc_11;
                    _loc_10 = new SPlayerItem();
                    _loc_10.itemCode = _loc_11.itemInfo.item.code;
                    _loc_7.itemData = new ItemData(_loc_10);
                    _loc_2[_loc_10.itemCode] = _loc_7;
                }
                _loc_5++;
            }
            for each (_loc_7 in _loc_2)
            {
                
                _loc_1.push(_loc_7);
            }
            _loc_1.sortOn("code");
            _view.update({type:"updateMaterial", data:_loc_1});
            
        }

        private function updateMaterialBuy() : void
        {
            var _loc_1:* = Cache.instance.equipment.getMaterialBuyData(this._opertionType);
            _view.update({type:"updateMaterial", data:_loc_1});
            
        }

        private function updateMaterialJewel() : void
        {
            var _loc_1:* = Cache.instance.equipment.getJewelData(this._opertionType, EquipmentModule.EquipmentType_Stone);
            _loc_1.sortOn("code");
            _view.update({type:"updateMaterial", data:_loc_1});
            
        }

        private function updateCost(param1:Object = null) : void
        {
            if (_view)
            {
                _view.update({type:"updateCost", data:{moneyHave:cache.role.money.coinBind + cache.role.money.coin, cost:this._cost}});
            }
            
        }

        private function ReChooseJewels(event:DataEvent) : void
        {
            var _loc_2:ItemData = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:Array = null;
            var _loc_3:* = event.data;
            var _loc_4:* = Cache.instance.pack.backPackCache.getJewelItems();
            var _loc_5:* = _loc_3.jewelNum;
            this._choosedMaterial = [];
            _loc_6 = 0;
            while (_loc_6 < _loc_4.length)
            {
                
                _loc_2 = _loc_4[_loc_6] as ItemData;
                if (_loc_2.uid == _loc_3.firstJewelUid)
                {
                    _loc_8 = Math.min(_loc_2.itemAmount, _loc_5);
                    _loc_7 = 0;
                    while (_loc_7 < _loc_8)
                    {
                        
                        this._choosedMaterial[_loc_7] = _loc_2;
                        _loc_7++;
                    }
                }
                _loc_6++;
            }
            if (this._choosedMaterial.length > 0)
            {
                _loc_9 = Cache.instance.pack.backPackCache.getPropItems();
                _loc_6 = 0;
                while (_loc_6 < _loc_9.length)
                {
                    
                    _loc_2 = _loc_9[_loc_6] as ItemData;
                    if (_loc_3.propData && _loc_2.uid == _loc_3.propData.uid)
                    {
                        this._choosedMaterial[4] = _loc_2;
                    }
                    _loc_6++;
                }
                _view.update({type:"autoChooseMaterial", material:this._choosedMaterial});
            }
            
        }

        private function doAddHintLabel(event:DataEvent) : void
        {
            var _loc_2:* = event.data as String;
            (view as EquipmentModule).addHintLabel(_loc_2);
            
        }

        private function autoSelectedMaterial() : void
        {
            switch(this._opertionType)
            {
                case EquipmentModule.OperationType_Strenthen:
                {
                    this.selectMaterialStrenthen();
                    break;
                }
                case EquipmentModule.OperationType_Refine:
                {
                    this.selectMaterialRefine();
                    break;
                }
                case EquipmentModule.OperationType_Recast:
                {
                    this.selectMaterialRecast();
                    break;
                }
                case EquipmentModule.OperationType_MakeHole:
                {
                    this.selectMaterialMakeHole();
                    break;
                }
                case EquipmentModule.OperationType_EmbedJewel:
                {
                    this.selectMaterialEmbedJewel();
                    break;
                }
                case EquipmentModule.OperationType_RemoveJewel:
                {
                    this.selectMaterialRemoveJewel();
                    break;
                }
                case EquipmentModule.OperationType_EquipAdvance:
                {
                    this.selectMaterialEquipAdvance();
                    break;
                }
                case EquipmentModule.OperationType_EquipDecompose:
                {
                    this.selectMaterialEquipDecompose();
                    break;
                }
                case EquipmentModule.OperationType_ComposeItems:
                {
                    this.selectMaterialComposeItems();
                    break;
                }
                default:
                {
                    break;
                }
            }
            
        }

        private function selectMaterialStrenthen() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:ItemData = null;
            var _loc_5:Array = null;
            var _loc_8:Array = null;
            if (this._choosedEquipmentData.length == 0)
            {
                
            }
            var _loc_1:Array = [];
            var _loc_6:* = (this._choosedEquipmentData[0] as ItemData).itemExInfo["strengthen"];
            if (this._currentUpdateType == EItemUpdateType._EItemUpdateUpgrade && _loc_6 == 10)
            {
                MsgManager.addBroadCast("已强化至最高等级，无法继续强化！");
                
            }
            var _loc_7:* = cache.pack.backPackCache.getPropByType(EProp._EPropEquipStrengthenProp);
            for each (_loc_4 in _loc_7)
            {
                
                if (this._currentUpdateType == EItemUpdateType._EItemUpdateUpgrade && _loc_4.itemInfo.item.itemLevel == (_loc_6 + 1) || this._currentUpdateType == EItemUpdateType._EItemUpdateRefresh && _loc_4.itemInfo.item.itemLevel == _loc_6)
                {
                    _loc_1.push({item:_loc_4, bind:_loc_4.itemInfo.item.bind, level:_loc_4.itemInfo.item.itemLevel});
                }
            }
            _loc_1.sortOn("level");
            _loc_1.sortOn("bind");
            if (this._useBind)
            {
                _loc_1.sortOn("bind", Array.DESCENDING);
            }
            _loc_5 = [];
            if (_loc_1.length > 0)
            {
                _loc_5.push(_loc_1[0]["item"]);
            }
            if (_loc_5.length == 0)
            {
                _loc_5 = [];
                _loc_8 = ItemConfig.instance.getPropsByType(EProp._EPropEquipStrengthenProp);
                _loc_2 = 0;
                while (_loc_2 < _loc_8.length)
                {
                    
                    _loc_4 = new ItemData((_loc_8[_loc_2] as ItemInfo).item.code);
                    if (this._currentUpdateType == EItemUpdateType._EItemUpdateUpgrade && _loc_4.itemInfo.item.itemLevel == (_loc_6 + 1) && _loc_4.itemInfo.item.bind == EBind._EBindNo || this._currentUpdateType == EItemUpdateType._EItemUpdateRefresh && _loc_4.itemInfo.item.itemLevel == _loc_6 && _loc_4.itemInfo.item.bind == EBind._EBindNo)
                    {
                        _loc_5.push(_loc_4);
                    }
                    _loc_2++;
                }
                _view.update({type:"autoChooseMaterial", material:_loc_5, isEnough:false});
            }
            else
            {
                _view.update({type:"autoChooseMaterial", material:_loc_5, isEnough:true});
            }
            this.selectPropStrengthen();
            
        }

        private function selectPropStrengthen() : void
        {
            var _loc_4:ItemData = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            if (this.choosedEquipmentData.length == 0)
            {
                
            }
            if (this._useLuckTuneNum == 0)
            {
                _view.update({type:"autoChooseLuckTune", luckTune:[]});
                
            }
            var _loc_1:* = (this._choosedEquipmentData[0] as ItemData).itemExInfo["strengthen"];
            var _loc_2:* = cache.pack.backPackCache.getPropByType(EProp._EPropEquipStrengthenLuckRune);
            var _loc_3:Array = [];
            for each (_loc_4 in _loc_2)
            {
                
                if (_loc_4.category == ECategory._ECategoryProp && _loc_1 >= _loc_4.itemInfo.item.effect && _loc_1 <= _loc_4.itemInfo.item.effectEx)
                {
                    _loc_3.push({item:_loc_4, bind:_loc_4.itemInfo.item.bind, level:_loc_4.itemInfo.item.itemLevel});
                }
            }
            _loc_7 = [];
            if (_loc_3.length > 0)
            {
                _loc_3.sortOn("level");
                _loc_3.sortOn("bind");
                if (this._useBind)
                {
                    _loc_3.sortOn("bind", Array.DESCENDING);
                }
                _loc_7.push(_loc_3[0]["item"]);
                _view.update({type:"autoChooseLuckTune", luckTune:_loc_7});
            }
            else
            {
                this._useLuckTuneNum = 0;
                _view.update({type:"autoChooseLuckTune", luckTune:_loc_7});
            }
            
        }

        private function selectMaterialRefine() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:ItemData = null;
            var _loc_5:Array = null;
            var _loc_8:Array = null;
            if (this.choosedEquipmentData.length == 0)
            {
                
            }
            var _loc_1:Array = [];
            var _loc_6:* = (this._choosedEquipmentData[0] as ItemData).itemExInfo["refine"];
            if (this._currentUpdateType == EItemUpdateType._EItemUpdateUpgrade && _loc_6 == 10)
            {
                MsgManager.addBroadCast("已精炼至最高等级，无法继续精炼！");
                
            }
            var _loc_7:* = cache.pack.backPackCache.getPropByType(EProp._EPropEquipRefineProp);
            for each (_loc_4 in _loc_7)
            {
                
                if (_loc_4.category == ECategory._ECategoryProp)
                {
                    if (this._currentUpdateType == EItemUpdateType._EItemUpdateUpgrade && _loc_4.itemInfo.item.itemLevel == (_loc_6 + 1) || this._currentUpdateType == EItemUpdateType._EItemUpdateRefresh && _loc_4.itemInfo.item.itemLevel == _loc_6)
                    {
                        _loc_1.push({item:_loc_4, bind:_loc_4.itemInfo.item.bind, level:_loc_4.itemInfo.item.itemLevel});
                    }
                }
            }
            _loc_1.sortOn("level");
            _loc_1.sortOn("bind");
            if (this._useBind)
            {
                _loc_1.sortOn("bind", Array.DESCENDING);
            }
            _loc_5 = [];
            if (_loc_1.length > 0)
            {
                _loc_5.push(_loc_1[0]["item"]);
            }
            if (_loc_5.length == 0)
            {
                _loc_5 = [];
                _loc_8 = ItemConfig.instance.getPropsByType(EProp._EPropEquipRefineProp);
                _loc_2 = 0;
                while (_loc_2 < _loc_8.length)
                {
                    
                    _loc_4 = new ItemData((_loc_8[_loc_2] as ItemInfo).item.code);
                    if (this._currentUpdateType == EItemUpdateType._EItemUpdateUpgrade && _loc_4.itemInfo.item.itemLevel == (_loc_6 + 1) && _loc_4.itemInfo.item.bind == EBind._EBindNo || this._currentUpdateType == EItemUpdateType._EItemUpdateRefresh && _loc_4.itemInfo.item.itemLevel == _loc_6 && _loc_4.itemInfo.item.bind == EBind._EBindNo)
                    {
                        _loc_5.push(_loc_4);
                        break;
                    }
                    _loc_2++;
                }
                _view.update({type:"autoChooseMaterial", material:_loc_5, isEnough:false});
            }
            else
            {
                _view.update({type:"autoChooseMaterial", material:_loc_5, isEnough:true});
            }
            this.selectPropRefine();
            
        }

        private function selectPropRefine() : void
        {
            var _loc_4:ItemData = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            if (this.choosedEquipmentData.length == 0)
            {
                
            }
            if (this._useLuckTuneNum == 0)
            {
                _view.update({type:"autoChooseLuckTune", luckTune:[]});
                
            }
            var _loc_1:* = (this._choosedEquipmentData[0] as ItemData).itemExInfo["refine"];
            var _loc_2:* = cache.pack.backPackCache.getPropByType(EProp._EPropEquipRefineLuckRune);
            var _loc_3:Array = [];
            for each (_loc_4 in _loc_2)
            {
                
                if (_loc_4.category == ECategory._ECategoryProp && _loc_1 >= _loc_4.itemInfo.item.effect && _loc_1 <= _loc_4.itemInfo.item.effectEx)
                {
                    _loc_3.push({item:_loc_4, bind:_loc_4.itemInfo.item.bind, level:_loc_4.itemInfo.item.itemLevel});
                }
            }
            _loc_7 = [];
            if (_loc_3.length > 0)
            {
                _loc_3.sortOn("level");
                _loc_3.sortOn("bind");
                if (this._useBind)
                {
                    _loc_3.sortOn("bind", Array.DESCENDING);
                }
                _loc_7.push(_loc_3[0]["item"]);
                _view.update({type:"autoChooseLuckTune", luckTune:_loc_7, isEnough:true});
            }
            else
            {
                this._useLuckTuneNum = 0;
                _view.update({type:"autoChooseLuckTune", luckTune:_loc_7, isEnough:false});
            }
            
        }

        private function selectMaterialRecast() : void
        {
            var _loc_1:Array = null;
            var _loc_2:int = 0;
            var _loc_3:ItemData = null;
            var _loc_4:ItemData = null;
            if (this.choosedEquipmentData.length == 0)
            {
                
            }
            _loc_1 = Cache.instance.pack.backPackCache.getPropItems();
            var _loc_5:Array = [];
            _loc_2 = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                _loc_3 = _loc_1[_loc_2] as ItemData;
                if (_loc_3.category == ECategory._ECategoryProp && _loc_3.itemInfo.item.type == EProp._EPropRefresh && _loc_3.itemInfo.item.color == (this._choosedEquipmentData[0] as ItemData).itemInfo.item.color)
                {
                    _loc_5.push({data:_loc_3, bind:_loc_3.itemInfo.item.bind});
                }
                _loc_2++;
            }
            if (this._useBind)
            {
                _loc_5.sortOn("bind", Array.DESCENDING);
            }
            else
            {
                _loc_5.sortOn("bind");
            }
            _loc_4 = _loc_5.length > 0 ? (_loc_5[0]["data"]) : (null);
            if (_loc_4)
            {
                _view.update({type:"autoChooseMaterial", material:[_loc_4]});
            }
            else
            {
                _view.update({type:"autoChooseMaterial", material:[null]});
            }
            
        }

        private function selectMaterialMakeHole() : void
        {
            var _loc_1:Object = null;
            var _loc_2:String = null;
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:ItemData = null;
            var _loc_6:ItemData = null;
            var _loc_11:int = 0;
            if (this.choosedEquipmentData.length == 0)
            {
                
            }
            var _loc_7:* = this._choosedEquipmentData[0] as ItemData;
            var _loc_8:* = (this._choosedEquipmentData[0] as ItemData).itemExInfo["hole1"];
            var _loc_9:* = _loc_7.itemExInfo["hole2"];
            var _loc_10:* = _loc_7.itemExInfo["hole3"];
            if (_loc_8 == 0)
            {
                _loc_11 = 1;
            }
            else if (_loc_9 == 0)
            {
                _loc_11 = 2;
            }
            else if (_loc_10 == 0)
            {
                _loc_11 = 3;
            }
            _loc_2 = ItemConfig.instance.getUpdateInfo(ItemConfig.WeaponMakeHole, _loc_11)["condition"];
            _loc_1 = JSON.deserialize(_loc_2);
            _loc_3 = Cache.instance.pack.backPackCache.getPropItems();
            _loc_4 = 0;
            while (_loc_4 < _loc_3.length)
            {
                
                _loc_5 = _loc_3[_loc_4] as ItemData;
                if (_loc_5.category == ECategory._ECategoryProp && _loc_5.itemInfo.item.type == EProp._EPropEquipMakeHoleRune && _loc_1["level"] == _loc_5.itemInfo.item.itemLevel)
                {
                    if (!_loc_6 || _loc_5.itemInfo.item.itemLevel < _loc_6.itemInfo.item.itemLevel || _loc_5.itemInfo.item.bind && this._useBind)
                    {
                        _loc_6 = _loc_5;
                    }
                }
                _loc_4++;
            }
            if (_loc_6)
            {
                _view.update({type:"autoChooseMaterial", material:[_loc_6]});
            }
            else
            {
                _view.update({type:"autoChooseMaterial", material:[null]});
            }
            
        }

        private function selectMaterialEmbedJewel() : void
        {
            var _loc_1:ItemData = null;
            var _loc_2:int = 0;
            var _loc_3:ItemData = null;
            var _loc_4:Object = null;
            if (!this._choosedMaterial)
            {
                
            }
            _loc_2 = 0;
            while (_loc_2 < this._choosedMaterial.length)
            {
                
                this._choosedMaterial[_loc_2].materialData = null;
                _loc_2++;
            }
            _loc_4 = this.getUsedMaterialNum();
            _loc_2 = 0;
            while (_loc_2 < this._choosedMaterial.length)
            {
                
                _loc_1 = this._choosedMaterial[_loc_2].jewelData;
                if (_loc_1 && !this._choosedMaterial[_loc_2].materialData)
                {
                    _loc_3 = this.getMaterialByJewel(_loc_1, _loc_4);
                    this._choosedMaterial[_loc_2].materialData = _loc_3;
                    if (_loc_3)
                    {
                        if (_loc_4[_loc_3.uid])
                        {
                            var _loc_5:* = _loc_4;
                            var _loc_6:* = _loc_3.uid;
                            var _loc_7:* = _loc_4[_loc_3.uid] + 1;
                            _loc_5[_loc_6] = _loc_7;
                        }
                        else
                        {
                            _loc_4[_loc_3.uid] = 1;
                        }
                    }
                }
                _loc_2++;
            }
            _view.update({type:"autoChooseMaterial", material:this._choosedMaterial});
            
        }

        private function selectMaterialRemoveJewel() : void
        {
            var _loc_1:ItemData = null;
            var _loc_2:int = 0;
            var _loc_3:ItemData = null;
            var _loc_4:Object = null;
            if (!this._choosedMaterial)
            {
                
            }
            _loc_4 = this.getUsedMaterialNum();
            _loc_2 = 0;
            while (_loc_2 < this._choosedMaterial.length)
            {
                
                _loc_1 = this._choosedMaterial[_loc_2].jewelData;
                if (_loc_1 && !this._choosedMaterial[_loc_2].materialData)
                {
                    _loc_3 = this.getMaterialByJewel(this._choosedMaterial[_loc_2].jewelData, _loc_4);
                    this._choosedMaterial[_loc_2].material = _loc_3;
                    if (_loc_3)
                    {
                        if (_loc_4[_loc_3.uid] != null)
                        {
                            var _loc_5:* = _loc_4;
                            var _loc_6:* = _loc_3.uid;
                            var _loc_7:* = _loc_4[_loc_3.uid] + 1;
                            _loc_5[_loc_6] = _loc_7;
                        }
                        else
                        {
                            _loc_4[_loc_3.uid] = 1;
                        }
                    }
                }
                else
                {
                    this._choosedMaterial[_loc_2].material = null;
                }
                _loc_2++;
            }
            _view.update({type:"autoChooseMaterial", material:this._choosedMaterial});
            
        }

        private function selectMaterialEquipAdvance() : void
        {
            var _loc_8:int = 0;
            var _loc_9:ItemData = null;
            var _loc_12:int = 0;
            var _loc_17:ItemData = null;
            if (this.choosedEquipmentData.length == 0)
            {
                
            }
            var _loc_1:* = this._choosedEquipmentData[0] as ItemData;
            var _loc_2:* = EquipAdvanceConfig.instance.getAdvanceConfig((_loc_1.itemInfo.extend as TItemWeapon).type);
            var _loc_3:* = new ItemData(_loc_2.specialMaterialItemCode);
            var _loc_4:* = new ItemData(_loc_2.normalMaterialItemCode);
            var _loc_5:Array = [];
            var _loc_6:Array = [];
            var _loc_7:* = Cache.instance.pack.backPackCache.getPropItems();
            var _loc_10:Array = [];
            var _loc_11:Array = [];
            _loc_8 = 0;
            while (_loc_8 < _loc_7.length)
            {
                
                _loc_9 = _loc_7[_loc_8] as ItemData;
                if (_loc_9.category != ECategory._ECategoryProp)
                {
                }
                else if ((_loc_9.itemCode == _loc_3.itemCode || _loc_9.itemCode == _loc_3.itemInfo.item.codeUnbind) && _loc_9.itemInfo.item.effect <= _loc_1.itemInfo.item.level && _loc_9.itemInfo.item.effectEx >= _loc_1.itemInfo.item.level)
                {
                    _loc_10.push({bind:_loc_9.itemInfo.item.bind, data:_loc_9});
                }
                else if ((_loc_9.itemCode == _loc_4.itemCode || _loc_9.itemCode == _loc_4.itemInfo.item.codeUnbind) && _loc_9.itemInfo.item.effect <= _loc_1.itemInfo.item.level && _loc_9.itemInfo.item.effectEx >= _loc_1.itemInfo.item.level)
                {
                    _loc_11.push({bind:_loc_9.itemInfo.item.bind, data:_loc_9});
                }
                _loc_8++;
            }
            if (this._useBind)
            {
                _loc_10.sortOn("bind", Array.DESCENDING);
                _loc_11.sortOn("bind", Array.DESCENDING);
            }
            else
            {
                _loc_10.sortOn("bind");
                _loc_11.sortOn("bind");
            }
            var _loc_13:Boolean = false;
            var _loc_14:* = new ItemData(_loc_3.itemInfo.item.codeUnbind);
            new ItemData(_loc_3.itemInfo.item.codeUnbind).itemAmount = _loc_2.specialMaterialNum;
            if (_loc_10.length > 0)
            {
                _loc_12 = 0;
                _loc_8 = 0;
                while (_loc_8 < _loc_10.length)
                {
                    
                    _loc_12 = _loc_12 + (_loc_10[_loc_8].data as ItemData).itemAmount;
                    _loc_6.push(_loc_10[_loc_8].data);
                    if (_loc_12 >= _loc_2.specialMaterialNum)
                    {
                        _loc_13 = true;
                        break;
                    }
                    _loc_8++;
                }
            }
            _loc_5.push({enough:_loc_13, data:_loc_14});
            if (!_loc_13)
            {
            }
            var _loc_15:Boolean = false;
            var _loc_16:* = new ItemData(_loc_4.itemInfo.item.codeUnbind);
            new ItemData(_loc_4.itemInfo.item.codeUnbind).itemAmount = _loc_2.normalMaterialNum;
            if (_loc_11.length > 0)
            {
                _loc_12 = 0;
                _loc_8 = 0;
                while (_loc_8 < _loc_11.length)
                {
                    
                    _loc_12 = _loc_12 + (_loc_11[_loc_8].data as ItemData).itemAmount;
                    _loc_6.push(_loc_11[_loc_8].data);
                    if (_loc_12 >= _loc_2.normalMaterialNum)
                    {
                        _loc_15 = true;
                        break;
                    }
                    _loc_8++;
                }
            }
            _loc_5.push({enough:_loc_15, data:_loc_16});
            if (!_loc_15)
            {
            }
            _view.update({type:"autoChooseMaterial", material:_loc_5, useItems:_loc_6});
            var _loc_18:Array = [];
            _loc_7 = cache.pack.backPackCache.getPropByType(EProp._EPropLingBaoDaemon);
            for each (_loc_9 in _loc_7)
            {
                
                if (_loc_9.category == ECategory._ECategoryProp)
                {
                    if (_loc_9.itemInfo.item.effect == _loc_3.itemCode || _loc_9.itemInfo.item.effectEx == _loc_3.itemCode)
                    {
                        _loc_18.push({item:_loc_9, bind:_loc_9.itemInfo.item.bind, level:_loc_9.itemInfo.item.level});
                    }
                }
            }
            if (_loc_18.length > 0)
            {
                _loc_18.sortOn("level");
                _loc_18.sortOn("bind");
                if (this._useBind)
                {
                    _loc_18.sortOn("bind", Array.DESCENDING);
                }
                _loc_17 = _loc_18[0]["item"];
                this._useLuckTuneNum = 1;
                _view.update({type:"autoChooseLuckTune", luckTune:[_loc_17]});
            }
            else
            {
                this._useLuckTuneNum = 0;
                _view.update({type:"autoChooseLuckTune", luckTune:[]});
            }
            
        }

        private function selectMaterialEquipDecompose() : void
        {
            var _loc_1:Array = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:ItemData = null;
            var _loc_5:ItemInfo = null;
            var _loc_6:TempRefreshData = null;
            var _loc_9:int = 0;
            if (this.choosedEquipmentData.length == 0)
            {
                
            }
            var _loc_7:Array = [];
            var _loc_8:* = new Dictionary();
            _loc_1 = ItemConfig.instance.getPropsByType(EProp._EPropRefresh);
            _loc_3 = 0;
            while (_loc_3 < this._choosedEquipmentData.length)
            {
                
                _loc_4 = this._choosedEquipmentData[_loc_3] as ItemData;
                _loc_2 = 0;
                while (_loc_2 < _loc_1.length)
                {
                    
                    _loc_5 = _loc_1[_loc_2] as ItemInfo;
                    _loc_9 = _loc_4.itemExInfo["bind"] == EBind._EBindYes ? (EBind._EBindYes) : (EBind._EBindNo);
                    if (_loc_5.item.color == _loc_4.itemInfo.item.color && _loc_5.item.bind == _loc_9)
                    {
                        if (_loc_8[_loc_5.item.name])
                        {
                            _loc_6 = _loc_8[_loc_5.item.name] as TempRefreshData;
                            _loc_6.itemInfo = _loc_5;
                        }
                        else
                        {
                            _loc_6 = new TempRefreshData(_loc_5.item.code);
                            _loc_8[_loc_5.item.name] = _loc_6;
                        }
                    }
                    _loc_2++;
                }
                _loc_3++;
            }
            for each (_loc_6 in _loc_8)
            {
                
                _loc_7.push(_loc_6.itemData);
            }
            _view.update({type:"autoChooseMaterial", material:_loc_7, isEnough:true});
            
        }

        private function selectMaterialComposeItems() : void
        {
            view.update({type:"autoChooseMaterial", material:[], isEnough:false});
            
        }

        private function getMaterialByJewel(param1:ItemData, param2:Object) : ItemData
        {
            var _loc_5:ItemData = null;
            var _loc_6:ItemData = null;
            var _loc_7:int = 0;
            var _loc_9:int = 0;
            var _loc_3:* = param1.itemInfo.item.itemLevel;
            var _loc_4:* = Cache.instance.pack.backPackCache.getPropItems();
            var _loc_8:* = Cache.instance.pack.backPackCache.getPropItems().length;
            _loc_7 = 0;
            while (_loc_7 < _loc_8)
            {
                
                _loc_5 = _loc_4[_loc_7] as ItemData;
                if (this._opertionType == EquipmentModule.OperationType_EmbedJewel)
                {
                    _loc_9 = EProp._EPropEquipEmbedRune;
                }
                else if (this._opertionType == EquipmentModule.OperationType_RemoveJewel)
                {
                    _loc_9 = EProp._EPropEquipRemoveRune;
                }
                if (_loc_5.category == ECategory._ECategoryProp && _loc_5.itemInfo.item.type == _loc_9 && _loc_3 >= _loc_5.itemInfo.item["effect"] && _loc_3 <= _loc_5.itemInfo.item["effectEx"] && (!param2[_loc_5.uid] || param2[_loc_5.uid] < _loc_5.itemAmount))
                {
                    if (!_loc_6 || _loc_5.itemInfo.item.itemLevel < _loc_6.itemInfo.item.itemLevel || _loc_5.itemInfo.item.bind && this._useBind)
                    {
                        _loc_6 = _loc_5;
                    }
                }
                _loc_7++;
            }
            if (_loc_6)
            {
                if (_loc_6.itemInfo.item["effect"] > _loc_3)
                {
                    trace("选取了高级的材料");
                }
                return _loc_6;
            }
            else
            {
                return null;
            }
        }

        private function checkIsChoosed(param1:ItemData) : Boolean
        {
            var _loc_2:ItemData = null;
            var _loc_3:int = 0;
            while (_loc_3 < this._choosedEquipmentData.length)
            {
                
                _loc_2 = this._choosedEquipmentData[_loc_3] as ItemData;
                if (_loc_2.uid == param1.uid)
                {
                    return true;
                }
                _loc_3++;
            }
            return false;
        }

        private function getUsedMaterialNum() : Object
        {
            var _loc_3:ItemData = null;
            var _loc_1:Object = {};
            var _loc_2:int = 0;
            while (_loc_2 < this._choosedMaterial.length)
            {
                
                _loc_3 = this._choosedMaterial[_loc_2].materialData;
                if (_loc_3)
                {
                    if (_loc_1[_loc_3.uid])
                    {
                        var _loc_4:* = _loc_1;
                        var _loc_5:* = _loc_3.uid;
                        var _loc_6:* = _loc_1[_loc_3.uid] + 1;
                        _loc_4[_loc_5] = _loc_6;
                    }
                    else
                    {
                        _loc_1[_loc_3.uid] = 1;
                    }
                }
                _loc_2++;
            }
            return _loc_1;
        }

        private function onUpdateResult(event:DataEvent) : void
        {
            if (_view)
            {
                _view.update({type:"updateResut", resultId:event.data.resultId, autoUpdate:event.data.autoUpdate, resultPlayerItem:event.data.resultPlayerItem, itemUpdateResults:event.data.itemUpdateResults});
            }
            
        }

        private function onUpdateError(event:DataEvent) : void
        {
            if (_view)
            {
                _view.update({type:"updateError"});
            }
            
        }

        private function doUpdate(event:DataEvent) : void
        {
            var _loc_2:Boolean = false;
            this._choosedMaterial = [];
            if (event.data.operationId == EItemUpdateOper._EItemUpdateOperStrengthen || event.data.operationId == EItemUpdateOper._EItemUpdateOperRefine || event.data.operationId == EItemUpdateOper._EItemUpdateOperAdvance)
            {
                _loc_2 = event.data.flag == 0 ? (false) : (true);
                GameProxy.equipmentProxy.equipUpdate(event.data.operationId, event.data.equipmentPosType, event.data.equipUid, event.data.materials, event.data.autoUpdate, _loc_2, false, event.data.target);
            }
            else if (event.data.operationId == EItemUpdateOper._EItemUpdateOperRecast)
            {
                GameProxy.equipmentProxy.equipRefresh(event.data.equipmentPosType, event.data.equipmentUid, event.data.materialUid, event.data.operType);
            }
            else if (event.data.operationId == EItemUpdateOper._EItemUpdateDecompose)
            {
                GameProxy.equipmentProxy.equipDecompose(event.data.equipmentUid, event.data.materialUid);
            }
            else if (event.data.operationId == EItemUpdateOper._EItemUpdateRefreshPropCompose)
            {
                GameProxy.equipmentProxy.propRefreshCompose(event.data.materials, event.data.isAutoCompose);
            }
            else
            {
                GameProxy.equipmentProxy.itemUpdate_async(event.data.operationId, event.data.autoUpdate, event.data.equipmentPosType, event.data.materials, event.data.target, event.data.num, event.data.flag);
            }
            
        }

        public function showToPanel(param1:int = 0) : void
        {
            _view.update({type:"showToPanel", data:param1});
            
        }

        public function doCastHandler(event:DataEvent) : void
        {
            if (view.isHide)
            {
                view.show();
            }
            switch(event.type)
            {
                case EventName.Equipment_DoCast_Strengthen:
                {
                    this.showToPanel(EquipmentModule.OperationType_Strenthen);
                    if (event.data)
                    {
                        _view.update({type:"doCast", data:event.data as ItemData});
                    }
                    break;
                }
                case EventName.Equipment_DoCast_Refine:
                {
                    this.showToPanel(EquipmentModule.OperationType_Refine);
                    if (event.data)
                    {
                        _view.update({type:"doCast", data:event.data as ItemData});
                    }
                    break;
                }
                case EventName.Equipment_DoCast_Recast:
                {
                    this.showToPanel(EquipmentModule.OperationType_Recast);
                    if (event.data)
                    {
                        _view.update({type:"doCast", data:event.data as ItemData});
                    }
                    break;
                }
                case EventName.Equipment_DoCast_MakeHole:
                {
                    this.showToPanel(EquipmentModule.OperationType_MakeHole);
                    if (event.data)
                    {
                        _view.update({type:"doCast", data:event.data as ItemData});
                    }
                    break;
                }
                case EventName.Equipment_DoCast_EmbedJewel:
                {
                    this.showToPanel(EquipmentModule.OperationType_EmbedJewel);
                    if (event.data)
                    {
                        _view.update({type:"doCast", data:event.data as ItemData});
                    }
                    break;
                }
                case EventName.Equipment_DoCast_RemoveJewel:
                {
                    this.showToPanel(EquipmentModule.OperationType_RemoveJewel);
                    if (event.data)
                    {
                        _view.update({type:"doCast", data:event.data as ItemData});
                    }
                    break;
                }
                case EventName.Equipment_DoCast_ComposeJewel:
                {
                    this.showToPanel(EquipmentModule.OperationType_ComposeJewel);
                    if (event.data)
                    {
                        _view.update({type:"doCast", data:event.data as ItemData});
                    }
                    break;
                }
                default:
                {
                    break;
                }
            }
            
        }

        private function onGuideEquipBuildHandler(event:DataEvent) : void
        {
            if (_view && !_view.isHide)
            {
                (_view as EquipmentModule).checkGuide();
            }
            
        }

    }
}
